package com.lb.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.concurrent.CountDownLatch;

/**
 * 订阅topic 接受的消息处理
 * @author user
 */
@Slf4j
@Component
public class RedisSubReceiver {

    public void receiveMessage(String jsonMsg) {
        CountDownLatch latch = new CountDownLatch(1);

        log.info("[开始消费REDIS消息队列userInfo数据...]");
        try {
            System.out.println(jsonMsg);
            log.info("[userInfo] : {}", jsonMsg);
            log.info("[消费REDIS消息队列userInfo数据成功.]");
        } catch (Exception e) {
            log.error("[消费REDIS消息队列userInfo数据失败，失败信息:{}]", e.getMessage());
        }
        latch.countDown();

    }

    /**
     * 广播所有回复client用户
     */
    /*public void broadcast(String message) {
        for (ClientSession cs : ClientSessionManager.getInstance().getAllClientSessions()) {
            message = "redis 广播回复==" + message;
            ChannelFuture cf = cs.sendText(message);
            cf.addListener(new ChannelFutureListener() {
                @Override
                public void operationComplete(ChannelFuture channelFuture) throws Exception {
                    if (cf.isDone() && cf.isSuccess()) {
                        log.info("服务器响应成功");
                        // mspMessageService.addSuccessMessage(message);
                    } else {
                        log.error("客户端网络不通");
                        // mspMessageService.addFailedMessage(message, "客户端网络不通");
                    }
                }
            });
        }

    }*/

    /**
     * 推送单条消息
     *
     * @param message
     */
    /*public void sendSingleMsg(String message) {
        message = message.substring(message.indexOf("{"));
        ZblWsMessage messageJson = JSON.parseObject(message, ZblWsMessage.class);
        ClientSession cs = ClientSessionManager.getInstance().getByClientId(messageJson.getToClientId());
        if (cs == null) {
            //处理异常或客户不在线
            log.error("用户不在线==" + ResponseUtils.toJSONString(messageJson));
        } else {
            ChannelFuture cf = cs.sendText(messageJson);
            cf.addListener(new ChannelFutureListener() {
                @Override
                public void operationComplete(ChannelFuture channelFuture) throws Exception {
                    if (cf.isDone() && cf.isSuccess()) {
                        log.info("服务器响应成功");
                        mspMessageService.addSuccessMessage(messageJson);
                    } else {
                        log.error("客户端网络不通");
                        mspMessageService.addFailedMessage(messageJson, "客户端网络不通");
                    }
                }
            });
        }

    }*/

    public void sendSingleMsgNew(String message) {
        /*ZblWsMessage messageJson = JSON.parseObject(message, ZblWsMessage.class);
        ClientSession cs = ClientSessionManager.getInstance().getByClientId(messageJson.getToClientId());
        if (cs == null) {
            //处理异常或客户不在线
            //log.error("用户不在线=="+ResponseUtils.toJSONString(messageJson));
        } else {
            try {
                MsgHandler handler = msgHandlerFactory.getHandler(messageJson.getBizTagKey());
                if (handler == null) {
                    log.error("could not find web socket message handler, message:{}", message);
                } else {
                    handler.onMessage(cs.getSession(), messageJson);
                }
            } catch (ClientSessionNotFoundException ex) {
                mspMessageService.addFailedMessage(ex.getWsMsg(), ex.getMessage());
            } catch (Exception e) {
                log.error("process web socket message error, session.id:{}, message:{}, ex:{}",
                        cs.getSession().id().asLongText(), ResponseUtils.toJSONString(messageJson), ExceptionUtils.getStackTrace(e));
            }
        }*/
    }
}



